#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "OLED_Data.h"
#include "Serial.h"
#include "LED.h"
#include "Servo.h"
#include "Ds1302.h"
#include "Serial2.h"
#include "send_data.h"
#include "Dht11.h"
#include "onenet.h"
#include "esp8266.h"
#include "usart.h"
#include "zhuanhua.h"
#include "Serial4.h"



/*变量声明区*/
uint8_t RxData;			//定义用于接收串口数据的变量
uint8_t i=10;
extern uint8_t Serial_RxPacket[16];
extern uint8_t Serial_RxPacket1[3];
unsigned char temp,humi;
extern uint8_t Alarm1[6];
extern uint8_t Alarm2[6];
extern uint8_t Alarm3[6];
extern uint8_t Alarm4[6];
extern char* name1; 
extern char* name2;
extern char* name3; 
extern char* name4; 
extern char* name5; 
u8 take1;
u8 fuyong1;
u8 take2;
u8 fuyong2;
u8 take3;
u8 fuyong3;
u8 take4;
u8 fuyong4;

uint8_t Alarm_Flag[4]={0,0,0,0};

void Seg_Proc()
{	
	DHT11_Read_Data(&temp,&humi);
  send_data_via_uart_humi(humi);
	send_data_via_uart_temp(temp);
	
	DS1302_read_realTime();
	send_data_via_uart1(TimeData.year);
	send_data_via_uart2(TimeData.month);
	send_data_via_uart3(TimeData.day);
	send_data_via_uart4(TimeData.hour);
	send_data_via_uart5(TimeData.minute);
	send_data_via_uart6(TimeData.second);
	send_data_via_uart7(take1);
	send_data_via_uart8(fuyong1);
	switch(TimeData.second % 10) 
	{
		case 1:
			OneNet_SendData();
			ESP8266_Clear();	
		break;
		case 3:
			OneNet_SendData1();
			ESP8266_Clear();	
		break;
		case 5:
			OneNet_SendData2();
			ESP8266_Clear();	
		break;
		case 7:
			OneNet_SendData3();
			ESP8266_Clear();	
		break;
		case 9:
			OneNet_SendData4();
			ESP8266_Clear();	
		break;
	}

}

void Uart1()
{
	if(Serial_GetRxFlag()==1)
	{
		if(Serial_RxPacket1[0] == 0x0a)
		{	
			name1 = "阿莫西林";
			if(Serial_RxPacket1[1] == 0x01)
			fuyong1 = 1;
			if(Serial_RxPacket1[1] == 0x02)
			fuyong1 = 2;
			if(Serial_RxPacket1[1] == 0x03)
			fuyong1 = 3;
			if(Serial_RxPacket1[2] == 0x21)
			take1 = 1;
			if(Serial_RxPacket1[2] == 0x22)
			take1 = 2;
			if(Serial_RxPacket1[2] == 0x23)
			take1 = 3;
		}
		
		if(Serial_RxPacket1[0] == 0x0b)
		{	
			name2 = "布洛芬";
			if(Serial_RxPacket1[1] == 0x01)
			fuyong2 = 1;
			if(Serial_RxPacket1[1] == 0x02)
			fuyong2 = 2;
			if(Serial_RxPacket1[1] == 0x03)
			fuyong2 = 3;
			if(Serial_RxPacket1[2] == 0x21)
			take2 = 1;
			if(Serial_RxPacket1[2] == 0x22)
			take2 = 2;
			if(Serial_RxPacket1[2] == 0x23)
			take2 = 3;
		}
		
		if(Serial_RxPacket1[0] == 0x0c)
		{	
			name3 = "降压药";
			if(Serial_RxPacket1[1] == 0x01)
			fuyong3 = 1;
			if(Serial_RxPacket1[1] == 0x02)
			fuyong3 = 2;
			if(Serial_RxPacket1[1] == 0x03)
			fuyong3 = 3;
			if(Serial_RxPacket1[2] == 0x21)
			take3 = 1;
			if(Serial_RxPacket1[2] == 0x22)
			take3 = 2;
			if(Serial_RxPacket1[2] == 0x23)
			take3 = 3;
		}

		if(Serial_RxPacket1[0] == 0x0d)
		{	
			name4= "降糖药";
			if(Serial_RxPacket1[1] == 0x01)
			fuyong4 = 1;
			if(Serial_RxPacket1[1] == 0x02)
			fuyong4 = 2;
			if(Serial_RxPacket1[1] == 0x03)
			fuyong4 = 3;
			if(Serial_RxPacket1[2] == 0x21)
			take4 = 1;
			if(Serial_RxPacket1[2] == 0x22)
			take4 = 2;
			if(Serial_RxPacket1[2] == 0x23)
			take4 = 3;
		}		
	}
}

void Uart2()
{
	if(Serial_GetRxFlag2()==1)
	{	
		Alarm(Serial_RxPacket);
		Servo_SetAngle(90);
	}	
	
	if(Alarm_Flag[0])
	{
		if((TimeData.hour == Alarm1[0] && TimeData.minute==Alarm1[1])||(TimeData.hour==Alarm1[2] && TimeData.minute==Alarm1[3])||( TimeData.hour==Alarm1[4]&& TimeData.minute==Alarm1[5]))
		{
			if(TimeData.second == 5)
			{
				uint8_t arry[3];
				arry[0] = 0x01;
				arry[1] =	take1; 
				arry[2] = fuyong1;
				Serial_SendArray4(arry,3);
			}
		}
	}
	if(Alarm_Flag[1])
	{
		if((TimeData.hour == Alarm2[0] && TimeData.minute==Alarm2[1])||(TimeData.hour==Alarm2[2] && TimeData.minute==Alarm2[3])||(TimeData.hour==Alarm2[4] && TimeData.minute==Alarm2[5]))
		{
			if(TimeData.second == 5)
			{
			uint8_t arry[3];
			arry[0] = 0x02;
			arry[1] =	fuyong2; 
			arry[2] = take2;
			Serial_SendArray4(arry,3);
			}
		}
	}
	if(Alarm_Flag[2])
	{
		if((TimeData.hour == Alarm3[0] && TimeData.minute==Alarm3[1])||(TimeData.hour==Alarm3[2] && TimeData.minute==Alarm3[3])||(TimeData.hour==Alarm3[4]&& TimeData.minute==Alarm3[5]))
		{
			if(TimeData.second == 5)
			{
				uint8_t arry[3];
				arry[0] = 0x03;
				arry[1] =	fuyong3; 
				arry[2] = take3;
				Serial_SendArray4(arry,3);
			}
		}
	}
	if(Alarm_Flag[3])
	{
		if((TimeData.hour == Alarm4[0] && TimeData.minute==Alarm4[1])||(TimeData.hour==Alarm4[2] && TimeData.minute==Alarm4[3])||(TimeData.hour==Alarm4[4]&& TimeData.minute==Alarm4[5]))
		{
			if(TimeData.second == 5)
			{
				uint8_t arry[3];
				arry[0] = 0x04;
				arry[1] =	fuyong4; 
				arry[2] = take4;
				Serial_SendArray4(arry,3);
			}
		}
	}
}

int main(void)
{
	
	LED_Init();
	OLED_Init();		
	DS1302_GPIO_Init();
	Serial_Init();	
	Serial_Init2();
	Serial_Init4();
	Usart3_Init();
	DHT11_Init();
	Servo_Init();
	ESP8266_Init();
	
	while(OneNet_DevLink())			//接入OneNET
	Delay_ms(500);	
	Servo_SetAngle(0);
	while (1)
	{	
		
		Seg_Proc();
		Uart1();
		Uart2();
	}
}











